{% extends "ft/base.html" %}
{% block title %}天天生鲜-详情页{% endblock %}
{% block css %}
    <link rel="stylesheet" href="/static/ft/css/detail.css">
{% endblock %}
{% block content %}
    <div class="container">
        {% csrf_token %}
        <!--2.搜索框开始-->
        <div class="search col-sm-12">
            <div class="row">
                <!--左边图片和文字-->
                <div class="col-lg-5">
                    <img src="/static/ft/img/logo.png" alt="..." class="img-rounded">
                    <span>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;商品详情</span>
                </div>
                <!--右边表单-->
                <div class="col-lg-6 col-lg-offset-1 search_bar">
                    <form action="">
                        <div class="input-group">
                            <input type="text" class="form-control" placeholder="搜索商品">
                            <span class="input-group-btn">
                    <button class="btn btn-success" style="padding: 6px 50px" type="submit">搜索</button>
                </span>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <menu class="container">
            <div class="col-xs-12 col-sm-2">
                <ul class="list-group">
                    <li class="list-group-item active">全部商品分类</li>
                </ul>

            </div>
            <div class="col-xs-12 col-sm-10">
                <ul class="nav nav-tabs">
                    <li role="presentation" class="active"><a href="#">首页</a></li>
                    <li role="presentation"><a href="#">手机生鲜</a></li>
                    <li role="presentation"><a href="#">抽奖</a></li>
                </ul>
            </div>
        </menu>
        <article class="container">
            <p class="col-xs-12">全部分类 > {{ goods.goodstype.name }} > 商品详情</p>
        </article>
        <section class="container">
            <div class="col-xs-12 col-sm-5">
                <img src="/static/bg/img/{{ goods.picture }}"/>
            </div>
            <div class="col-xs-12 col-sm-7">
                <h2 class="title">{{ goods.name }}</h2>
                <p>{{ goods.description }}</p>
                <div class="panel panel-danger">
                    <div class="panel-heading">
                        <h1 id="price_detail">¥{{ goods.price }}<span class="span1">{{ goods.unite }}--{{ goods.number }}</span></h1>
                    </div>
                </div>
                <div class="col-xs-2">
                    <p class="newheight">数量:</p>
                </div>
                <div class="input-group col-xs-3">
                    <span class="input-group-addon" id="btn_add">+</span>
                    <input type="text" id="my_goods_num" class="form-control newwidth" value="1">
                    <span class="input-group-addon" id="btn_sub">-</span>
                </div>
                <div class="col-xs-12">
                    <p>总价:<span class="newprice" id="newprice">{{ goods.price }}元</span></p>
                </div>
                <p class="col-xs-3"><a class="btn btn-danger btn-lg" href="#" role="button">立即购买</a></p>
                <p class="col-xs-3"><a id="my_cart_add" goods_id="{{ goods.id }}" class="btn btn-danger btn-lg" href="bootstrap_cart.html" role="button">加入购物车</a>
                </p>
            </div>
        </section>
        <section class="container newspace">
            <div class="col-xs-12 col-sm-2">
                <div class="panel panel-success">
                    <div class="panel-heading">
                        <h3 class="panel-title text-center newheight">新品推荐</h3>
                    </div>
                    <div class="panel-body">
                        {% for goods in goods_list %}
                            <div class="col-xs-12 col-sm-12">
                                <a href="#" class="thumbnail">
                                    <img src="/static/bg/img/{{ goods.picture }}" class="img-responsive">
                                    <p class="text-center">{{ goods.name }}</p>
                                    <p class="text-center">￥{{ goods.price }}</p>
                                </a>
                            </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
            <div class="col-xs-12 col-sm-10">

                <!-- Nav tabs -->
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#home" data-toggle="tab">商品介绍</a></li>
                    <li><a href="#profile" data-toggle="tab">评论</a></li>
                </ul>

                <!-- Tab panes -->
                <div class="tab-content">
                    <div class="tab-pane active" id="home">
                        <h3>商品详情</h3>
                        <p>
                            {{ goods.detail }}
                        </p>
                    </div>
                    <div class="tab-pane" id="profile">...</div>
                </div>

            </div>
        </section>

    </div>
{% endblock %}
{% block js %}
    <script>
        $(function () {
            $("#btn_add").click(function () {
                var value = $(this).next().val();
                value = parseInt(value);
                value += 1;
                $(this).next().val(value);
                /*取价格的html文本单位*/
                var htmls = $("#price_detail").html()
                /*用span进行切分,取出价格，转换成float型*/
                prices = htmls.split("span");
                price = prices[0];
                price = price.substring(1, price.length - 1);
                price = parseFloat(price);
                total = value * price;
                total = total.toFixed(2);
                /*求总价修改总价栏*/
                $("#newprice").text(total + "元")
            });
            $("#btn_sub").click(function () {
                var value = $(this).prev().val();
                value = parseInt(value);
                value -= 1;
                value = value <= 1 ? 1 : value;
                $(this).prev().val(value);
                /*取价格的html文本单位*/
                var htmls = $("#price_detail").html()
                /*用span进行切分,取出价格，转换成float型*/
                prices = htmls.split("span");
                price = prices[0];
                price = price.substring(1, price.length - 1);
                price = parseFloat(price);
                total = value * price;
                /*求总价修改总价栏*/
                $("#newprice").text(total.toFixed(2) + "元")
            });
        })
    </script>
    <script>
     $("#my_cart_add").click(function () {
            //商品id
            var goods_id = $(this).attr("goods_id")
            //商品数量
            var goods_num = $("#my_goods_num").val()
            //csrf
            var csrfmiddlewaretoken = $("[name=csrfmiddlewaretoken]").val()
            $.ajax({
                url: "/ft/cart/add/",//路由
                method: "post",//post请求,csrf会拦截
                data: {//参数
                    goods_id: goods_id,
                    goods_num: goods_num,
                    csrfmiddlewaretoken: csrfmiddlewaretoken//为了防止csrf拦截 1、需要加入csrf标签 2、获取值
                },
                success: function (data) {//成功回调函数
                    if (data["code"] == 0) {
                        alert("添加成功，在购物车等亲~")
                    } else if (data["code"] == 1) {
                        alert("数据不完整亲~")
                    } else if (data["code"] == 2) {
                        alert("库存不足亲~")
                    }else if (data["code"] ==3) {
                        alert("异常~")
                    }
                },
                error: function () {
                    alert("error...")
                }
            })
            return false
        })
    </script>
{% endblock %}